package com.whj.handler;

import com.whj.common.agreement.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class ClientHandler extends ChannelInboundHandlerAdapter implements Callable {
    private ChannelHandlerContext context; //发送数据的对象
    private String result; //服务端发来的数据
    private Object param; //要给服务端发的数据 RpcRequest对象
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        this.context = ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        this.result = msg.toString();
        System.out.println("服务端："+this.result);
        notify();
    }

    @Override
    public synchronized Object call() throws Exception {
        this.context.writeAndFlush(this.param);
        wait();
        return this.result;
    }

    public void setParam(Object obj) {
        this.param = obj;
    }
}
